package zzz.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sun.corba.se.impl.protocol.AddressingDispositionException;

import zzz.model.Post;
import zzz.model.User;
import zzz.service.PostService;
import zzz.service.UserService;

@Controller
@RequestMapping("/postmanage")
public class ManagePostAction {
	@Autowired
	private PostService postService;
	@RequestMapping("/managepost")
	public String managePost(Map<String,List<Post>> m){
		List<Post> allPost=postService.findAllPost();
		//System.out.println(allPost.toString());
		m.put("allPost", allPost);
		return "manager/postManager";
	}
	
	@RequestMapping("/delpost")
	public String manageUser(String id) {
		postService.deletePost(id);
		return "redirect:/postmanage/managepost";
	}
	
	@RequestMapping("/changePostPage")
	public String changeUserPage(@RequestParam String postId,Map<String, Post> m) {
		Post changePost=postService.findPostById(postId);
		System.out.println("change"+changePost.toString());
		m.put("changePost", changePost);
		return "manager/postChangePage";
	}
	@RequestMapping("/updatepost")
	public String updatePost(Post post,Map<String,String> m) {
		Post changePost=postService.findPostById(String.valueOf(post.getPostId()));
		System.out.println("sss"+post);
		post.setPostTime(changePost.getPostTime());
		int i=postService.updatePost(post);
		String result=null;
		if(i!=0){
			m.put("msg", "�޸ĳɹ�");
			m.put("herf", "managepost");
		}else{
			m.put("msg", "�޸�ʧ��");
			m.put("herf", "managepost");
		}
		//return "redirect:/postmanage/managepost";
		return "common/msgModal";
	}
	@RequestMapping("/postManageAddPost")
	public String addPostPage() {
		return "manager/postManageAddPost";
	}
	@RequestMapping("/manageraddpost")
	public String addPost(Map<String, String> m,Post post) {
		System.out.println(post);
		Date date=new Date();
		post.setPostTime(date);
		int i=postService.addPost(post);
		if(i!=0) {
			m.put("msg", "成功");
			m.put("herf", "managepost");
		}else {
			m.put("msg", "失败");
			m.put("herf", "managepost");
		}
		return "common/msgModal";
	}
}
